package com.xxxx.rpc.rabbitmq;

import com.xxxx.common.utils.SendSms;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;

@Component
public class SmsProduce {
    @Resource
    RabbitTemplate rabbitTemplate;

    RabbitTemplate.ConfirmCallback confirmCallback = (correlationData, ack, cause) -> {
        /*Gson gson = new Gson();
        log.info(gson.toJson(correlationData));
        if (!ack) {
            log.warn(cause);
        }*/
    };

    RabbitTemplate.ReturnCallback returnCallback = (message, replyCode, replyText, exchange, routingKey) -> {
        System.err.println(replyCode);
        System.err.println(replyText);
        System.err.println(exchange);
        System.err.println(routingKey);
    };

    public Long sendToQueue(SendSms sms) {
        long begin = System.currentTimeMillis();
        rabbitTemplate.setConfirmCallback(confirmCallback);
        rabbitTemplate.setReturnCallback(returnCallback);
        CorrelationData cd = new CorrelationData();
        cd.setId(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("email_exchange", "test.email", sms, cd);
        long end = System.currentTimeMillis();
        return end - begin;
    }
}
